home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxctable.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.3 KB  |  148 lines

  1. /* Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxctable.c,v 1.3 2000/09/19 19:00:35 lpd Exp $ */
  20. /* Color table lookup and interpolation */
  21. #include "gx.h"
  22. #include "gxfixed.h"
  23. #include "gxfrac.h"
  24. #include "gxctable.h"
  25.  
  26. /* See gxctable.h for the API and structure definitions. */
  27.  
  28. /*
  29.  * Define an implementation that simply picks the nearest value without
  30.  * any interpolation.
  31.  */
  32. void
  33. gx_color_interpolate_nearest(const fixed * pi,
  34.                  const gx_color_lookup_table * pclt, frac * pv)
  35. {
  36.     const int *pdim = pclt->dims;
  37.     int m = pclt->m;
  38.     const gs_const_string *table = pclt->table;
  39.  
  40.     if (pclt->n > 3) {
  41.     table += fixed2int_var_rounded(pi[0]) * pdim[1];
  42.     ++pi, ++pdim;
  43.     } {
  44.     int ic = fixed2int_var_rounded(pi[2]);
  45.     int ib = fixed2int_var_rounded(pi[1]);
  46.     int ia = fixed2int_var_rounded(pi[0]);
  47.     const byte *p = pclt->table[ia].data + (ib * pdim[2] + ic) * m;
  48.     int j;
  49.  
  50.     for (j = 0; j < m; ++j, ++p)
  51.         pv[j] = byte2frac(*p);
  52.     }
  53. }
  54.  
  55. /*
  56.  * Define an implementation that uses trilinear interpolation.
  57.  */
  58. private void
  59. interpolate_accum(const fixed * pi, const gx_color_lookup_table * pclt,
  60.           frac * pv, fixed factor)
  61. {
  62.     const int *pdim = pclt->dims;
  63.     int m = pclt->m;
  64.  
  65.     if (pclt->n > 3) {
  66.     /* Do two 3-D interpolations, interpolating between them. */
  67.     gx_color_lookup_table clt3;
  68.     int ix = fixed2int_var(pi[0]);
  69.     fixed fx = fixed_fraction(pi[0]);
  70.  
  71.     clt3.n = 3;
  72.     clt3.dims[0] = pdim[1];    /* needed only for range checking */
  73.     clt3.dims[1] = pdim[2];
  74.     clt3.dims[2] = pdim[3];
  75.     clt3.m = m;
  76.     clt3.table = pclt->table + ix * pdim[1];
  77.     interpolate_accum(pi + 1, &clt3, pv, fixed_1);
  78.     if (ix == pdim[0] - 1)
  79.         return;
  80.     clt3.table += pdim[1];
  81.     interpolate_accum(pi + 1, &clt3, pv, fx);
  82.     } else {
  83.     int ic = fixed2int_var(pi[2]);
  84.     fixed fc = fixed_fraction(pi[2]);
  85.     uint dc1 = (ic == pdim[2] - 1 ? 0 : m);
  86.     int ib = fixed2int_var(pi[1]);
  87.     fixed fb = fixed_fraction(pi[1]);
  88.     uint db1 = (ib == pdim[1] - 1 ? 0 : pdim[2] * m);
  89.     uint dbc = (ib * pdim[2] + ic) * m;
  90.     uint dbc1 = db1 + dc1;
  91.     int ia = fixed2int_var(pi[0]);
  92.     fixed fa = fixed_fraction(pi[0]);
  93.     const byte *pa0 = pclt->table[ia].data + dbc;
  94.     const byte *pa1 =
  95.         (ia == pdim[0] - 1 ? pa0 : pclt->table[ia + 1].data + dbc);
  96.     int j;
  97.  
  98.     /* The values to be interpolated are */
  99.     /* pa{0,1}[{0,db1,dc1,dbc1}]. */
  100.     for (j = 0; j < m; ++j, ++pa0, ++pa1) {
  101.         frac v000 = byte2frac(pa0[0]);
  102.         frac v001 = byte2frac(pa0[dc1]);
  103.         frac v010 = byte2frac(pa0[db1]);
  104.         frac v011 = byte2frac(pa0[dbc1]);
  105.         frac v100 = byte2frac(pa1[0]);
  106.         frac v101 = byte2frac(pa1[dc1]);
  107.         frac v110 = byte2frac(pa1[db1]);
  108.         frac v111 = byte2frac(pa1[dbc1]);
  109.         frac rv;
  110.  
  111.         frac v00 = v000 +
  112.         (frac) arith_rshift((long)fc * (v001 - v000),
  113.                     _fixed_shift);
  114.         frac v01 = v010 +
  115.         (frac) arith_rshift((long)fc * (v011 - v010),
  116.                     _fixed_shift);
  117.         frac v10 = v100 +
  118.         (frac) arith_rshift((long)fc * (v101 - v100),
  119.                     _fixed_shift);
  120.         frac v11 = v110 +
  121.         (frac) arith_rshift((long)fc * (v111 - v110),
  122.                     _fixed_shift);
  123.  
  124.         frac v0 = v00 +
  125.         (frac) arith_rshift((long)fb * (v01 - v00),
  126.                     _fixed_shift);
  127.         frac v1 = v10 +
  128.         (frac) arith_rshift((long)fb * (v11 - v10),
  129.                     _fixed_shift);
  130.  
  131.         rv = v0 +
  132.         (frac) arith_rshift((long)fa * (v1 - v0),
  133.                     _fixed_shift);
  134.         if (factor == fixed_1)
  135.         pv[j] = rv;
  136.         else
  137.         pv[j] += (frac) arith_rshift((long)factor * (rv - pv[j]),
  138.                          _fixed_shift);
  139.     }
  140.     }
  141. }
  142. void
  143. gx_color_interpolate_linear(const fixed * pi,
  144.                 const gx_color_lookup_table * pclt, frac * pv)
  145. {
  146.     interpolate_accum(pi, pclt, pv, fixed_1);
  147. }
  148.